package com.xgkj.diyiketang.activity.data;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.xgkj.diyiketang.EatShowApp;
import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.GouMaiKeChengActivity;
import com.xgkj.diyiketang.activity.ZhiboPasswordActivity;
import com.xgkj.diyiketang.data.EatShowUserInfo;
import com.xgkj.diyiketang.util.ImageCache;
import com.xgkj.diyiketang.util.ViewUtil;
import com.xgkj.diyiketang.yizhibo.Constants;
import com.xgkj.diyiketang.yizhibo.PlayerNewActivity;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by XCKJ_Paladowow on 2016/10/21.
 */
public class ShouyeLiveAdapter extends BaseAdapter {
    private Context mContext;
    private List<EatShowUserInfo> mList = new ArrayList<EatShowUserInfo>();

    public ShouyeLiveAdapter(Context context,List<EatShowUserInfo> mList) {
        mContext = context;
        this.mList = mList;
    }

    public void updateData(List<EatShowUserInfo> list) {
        if (mList != null) {
            mList.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void clearData() {
        if (mList != null) {
            mList.clear();
        }
        notifyDataSetChanged();
    }

    public ArrayList<EatShowUserInfo> getDataList() {
        return (ArrayList<EatShowUserInfo>) mList;
    }


    @Override
    public int getCount() {
        if (mList != null) {
            return mList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (mList != null && mList.size() > 0) {
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ChildHolder childHolder = null;
        if (convertView == null || convertView.getTag() == null) {
            childHolder = new ChildHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.shouye_live_item, null);
            childHolder.fengmianImg = (ImageView) convertView.findViewById(R.id.fengmian);
            childHolder.liveTitle = (TextView) convertView.findViewById(R.id.live_title);
            childHolder.liveState = (TextView) convertView.findViewById(R.id.state);
            childHolder.charge_or_pwdText = (ImageButton) convertView.findViewById(R.id.charge_or_pwd);
            convertView.setTag(childHolder);
        } else {
            childHolder = (ChildHolder) convertView.getTag();
        }

        EatShowUserInfo info = mList.get(position);
        if (info != null) {
            String fengmian = info.getLiveCoverUrl();
            if (fengmian != null && fengmian.length() > 0) {
                ViewUtil.Size size = ViewUtil.getHeadImageSize(mContext, 165);
                Bitmap cacheBitmap = ImageCache.getInstance(mContext).getBitmapFromMemoryCache(ImageCache.getInstance(mContext).getCacheKey(fengmian, size));
                if (cacheBitmap != null) {
                    childHolder.fengmianImg.setImageBitmap(cacheBitmap);
                } else {
                    childHolder.fengmianImg.setImageResource(R.drawable.zhanweitu);
                    ImageCache.getInstance(mContext).loadBitmapToView(childHolder.fengmianImg, fengmian, size);
                }
            }

            String state = info.getLiveState();
            if (state != null && state.length() > 0) {
                if (state.equals("0")) {
                    childHolder.liveState.setText("直播");
                } else if (state.equals("1")) {
                    childHolder.liveState.setText("回放");
                }
            }

            if (info.getLiveTitle() != null) {
                try {
                    childHolder.liveTitle.setText(URLDecoder.decode(info.getLiveTitle(), "utf-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }

            final String vid = info.getVid();
            final String isPlayer = info.getIsMoive2();
            final String liveState = info.getLiveState();
            final int liveType = info.getLiveType();
            final String title = info.getLiveTitle();
            final String anchor = info.getNickName();
            final String headUrl = info.getUserLogo();
            final int isCharge = info.getIsCharge();
            final String fee = info.getFee();
            EatShowUserInfo userInfo = EatShowApp.getInstance().getmUserInfo();
            int vip = 0;
            String userId = null;
            if (userInfo != null) {
                userId = userInfo.getUserId();
                vip = userInfo.isVip();
            }
            String anchorId = info.getUserId();
            boolean isSelf = false;
            if (anchorId != null && anchorId.equals(userId)) {
                isSelf = true;
            }
            final boolean finalIsSelf = isSelf;
            if (liveType == 1) {
                childHolder.charge_or_pwdText.setImageResource(R.drawable.fufei);
            } else if (liveType == 2) {
                childHolder.charge_or_pwdText.setImageResource(R.drawable.simi);
            } else if (liveType == 3) {
                childHolder.charge_or_pwdText.setImageResource(R.drawable.vip);
            } else {
                childHolder.charge_or_pwdText.setImageResource(0);
            }
            final int finalVip = vip;
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (finalIsSelf || liveType == 0 || (liveType == 1 && isCharge == 1)) {
                        Intent intent = new Intent(mContext, PlayerNewActivity.class);
                        intent.putExtra(Constants.EXTRA_KEY_VIDEO_ID, vid);
                        intent.putExtra(Constants.LIVE_STATE, liveState);
                        intent.putExtra(Constants.ZHIBO_PLAY, isPlayer);
//                        intent.putExtra("ZHIBO_LIST", getDataList());
//                        intent.putExtra("CURRENT_POSITION", currentPosition);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        mContext.startActivity(intent);
                    } else if (liveType == 1 && isCharge == 0) {
                        //收费
                        Intent intent = new Intent(mContext, GouMaiKeChengActivity.class);
                        intent.putExtra("From", 1);
                        intent.putExtra("TITLE", title);
                        intent.putExtra("ZUOZHE", anchor);
                        intent.putExtra("FEE", fee);
                        intent.putExtra(Constants.ZHIBO_PLAY, isPlayer);
                        intent.putExtra(Constants.EXTRA_KEY_VIDEO_ID, vid);
                        intent.putExtra(Constants.LIVE_STATE, liveState);
//                        intent.putExtra("ZHIBO_LIST", getDataList());
//                        intent.putExtra("CURRENT_POSITION", currentPosition);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        mContext.startActivity(intent);
                    } else if (liveType == 2) {
                        //密码
                        Intent intent = new Intent(mContext, ZhiboPasswordActivity.class);
                        intent.putExtra("head_url", headUrl);
                        intent.putExtra("nickname", anchor);
                        intent.putExtra(Constants.ZHIBO_PLAY, isPlayer);
                        intent.putExtra(Constants.EXTRA_KEY_VIDEO_ID, vid);
                        intent.putExtra(Constants.LIVE_STATE, liveState);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        mContext.startActivity(intent);
//                        intent.putExtra("ZHIBO_LIST", getDataList());
//                        intent.putExtra("CURRENT_POSITION", currentPosition);
                    } else if (liveType == 3 && finalVip != 0) {
                        Intent intent = new Intent(mContext, PlayerNewActivity.class);
                        intent.putExtra(Constants.LIVE_STATE, liveState);
                        intent.putExtra(Constants.EXTRA_KEY_VIDEO_ID, vid);
                        intent.putExtra(Constants.ZHIBO_PLAY, isPlayer);
//                        intent.putExtra("ZHIBO_LIST", getDataList());
//                        intent.putExtra("CURRENT_POSITION", currentPosition);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        mContext.startActivity(intent);
                    } else if (liveType == 3 && finalVip == 0) {
                        Toast.makeText(mContext, "请购买VIP后观看", Toast.LENGTH_SHORT).show();
                    }
                }
            });


            /*final int currentPosition = position;
            final String vid = info.getVid();
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(EatShowApp.getInstance().getmUserInfo()==null){
                        mContext.startActivity(new Intent(mContext,LoginActivity.class));
                    }else {
                        Intent intent = new Intent(mContext, PlayerActivity.class);
                        intent.putExtra(Constants.EXTRA_KEY_VIDEO_ID, vid);
//                        intent.putExtra("ZHIBO_LIST", getDataList());
//                        intent.putExtra("CURRENT_POSITION", currentPosition);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        mContext.startActivity(intent);
                    }

                }
            });*/
        }

        return convertView;
    }

    class ChildHolder {
        ImageView fengmianImg;
        TextView liveTitle;
        TextView liveState;
        ImageButton charge_or_pwdText;
    }
}
